package ${packageName}.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koi.common.ex.koi.YUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import java.util.List;

/**
 * @description [<#if tableComment??>${tableComment}-控制层]</#if>
 * @author ${author}
 * @date ${date}
 */
@Validated
@RequestMapping("/${moduleFolder}")
@RequiredArgsConstructor
@RestController
public class ${tableNameBig}Controller {

    private final ${tableNameBig}Service ${tableNameSmall}Service;

    /**
     * @description [分页查询]
     * @author ${author}
     * @date ${date}
     */
    @GetMapping("/${tableNameSmall}/listPage")
    public IPage<${tableNameBig}> listPage(${tableNameBig}Vo vo) {
        // 分页构造器
        Page<${tableNameBig}> page = new Page<>(vo.getPageNo(),vo.getPageSize());
        // 条件构造器
        LambdaQueryWrapper<${tableNameBig}> wrapper = new LambdaQueryWrapper<>();
        <#if tableColumnList?exists>
        <#assign dateNeeded = 0>
        <#list tableColumnList as koiColumn>
        <#if koiColumn.isSearch == '0' && koiColumn.javaType == 'Date'>
        <#assign dateNeeded = dateNeeded + 1>
        </#if>
        <#if koiColumn.isSearch == '0' && koiColumn.javaField != 'createTime'>
        <#if koiColumn.javaType == 'String'>
        wrapper.${koiColumn.searchType}(StringUtils.isNotBlank(vo.get${koiColumn.javaField?cap_first}()), ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        <#elseif koiColumn.javaType == 'Integer' || koiColumn.javaType == 'Long'>
        wrapper.${koiColumn.searchType}(StringUtils.isNotBlank(vo.get${koiColumn.javaField?cap_first}), ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        <#elseif koiColumn.javaType == 'Date' || koiColumn.javaType == 'LocalDate' || koiColumn.javaType == 'LocalDateTime'>
        wrapper.${koiColumn.searchType}(vo.get${koiColumn.javaField?cap_first}() != null, ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        <#else>
        wrapper.${koiColumn.searchType}(vo.get${koiColumn.javaField?cap_first}() != null, ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        </#if>
        </#if>
        </#list>
        <#if dateNeeded != 0>
        wrapper.ge(vo.getBeginTime() != null, ${tableNameBig}::getCreateTime, vo.getBeginTime());
        wrapper.le(vo.getEndTime() != null, ${tableNameBig}::getCreateTime, vo.getEndTime());

        </#if>
        </#if>
        // 进行分页查询
        page = ${tableNameSmall}Service.page(page,wrapper);
        return page;
    }

    /**
     * @description [查询所有]
     * @author ${author}
     * @date ${date}
     */
    @GetMapping("/${tableNameSmall}/list")
    public List<${tableNameBig}> list(${tableNameBig}Vo vo) {
        // 条件构造器
        LambdaQueryWrapper<${tableNameBig}> wrapper = new LambdaQueryWrapper<>();
        <#if tableColumnList?exists>
        <#assign dateNeeded = 0>
        <#list tableColumnList as koiColumn>
        <#if koiColumn.isSearch == '0' && koiColumn.javaType == 'Date'>
        <#assign dateNeeded = dateNeeded + 1>
        </#if>
        <#if koiColumn.isSearch == '0' && koiColumn.javaField != 'createTime'>
        <#if koiColumn.javaType == 'String'>
        wrapper.${koiColumn.searchType}(StringUtils.isNotBlank(vo.get${koiColumn.javaField?cap_first}()), ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        <#elseif koiColumn.javaType == 'Integer' || koiColumn.javaType == 'Long'>
        wrapper.${koiColumn.searchType}(StringUtils.isNotBlank(vo.get${koiColumn.javaField?cap_first}), ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        <#elseif koiColumn.javaType == 'Date' || koiColumn.javaType == 'LocalDate' || koiColumn.javaType == 'LocalDateTime'>
        wrapper.${koiColumn.searchType}(vo.get${koiColumn.javaField?cap_first}() != null, ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        <#else>
        wrapper.${koiColumn.searchType}(vo.get${koiColumn.javaField?cap_first}() != null, ${tableNameBig}::get${koiColumn.javaField?cap_first}, vo.get${koiColumn.javaField?cap_first}());
        </#if>
        </#if>
        </#list>
        <#if dateNeeded != 0>
        wrapper.ge(vo.getBeginTime() != null, ${tableNameBig}::getCreateTime, vo.getBeginTime());
        wrapper.le(vo.getEndTime() != null, ${tableNameBig}::getCreateTime, vo.getEndTime());

        </#if>
        </#if>
        return ${tableNameSmall}Service.list(wrapper);
    }

    <#if tableTemplate == "tree">
    /**
     * @description [级联下拉框]
     * @author ･ᴗ･
     */
    @GetMapping("/${tableNameSmall}/cascaderList")
    public List<CascaderStringBo> cascaderList() {
        return ${tableNameSmall}Service.cascaderList();
    }
    </#if>

    /**
     * @description [查询一个]
     * @author ${author}
     * @date ${date}
     */
    @GetMapping("/${tableNameSmall}/getById/{id}")
    public ${tableNameBig} getById(@PathVariable Long id) {
        return ${tableNameSmall}Service.getById(id);
    }

    /**
     * @description [新增]
     * @author ${author}
     * @date ${date}
     */
    @KoiLog(title = "${tableComment}新增", operType = OperType.ADD)
    @SaCheckPermission("${moduleFolder}:${pageFolder}:add")
    @PostMapping("/${pageFolder}/add")
    public void add(@Validated @RequestBody ${tableNameBig} ${tableNameSmall}) {
        ${tableNameSmall}.setCreateBy(KoiLoginUtil.getUserName());
        YUtil.isTrue(!${tableNameSmall}Service.save(${tableNameSmall}), "新增失败，请稍后重试");
    }

    /**
     * @description [修改]
     * @author ${author}
     * @date ${date}
     */
    @KoiLog(title = "${tableComment}修改", operType = OperType.UPDATE)
    @SaCheckPermission("${moduleFolder}:${pageFolder}:update")
    @PostMapping("/${pageFolder}/update")
    public void update(@Validated @RequestBody ${tableNameBig} ${tableNameSmall}) {
        ${tableNameSmall}.setUpdateBy(KoiLoginUtil.getUserName());
        YUtil.isTrue(!${tableNameSmall}Service.updateById(${tableNameSmall}), "修改失败，请稍后重试");
    }

    /**
     * @description [删除]
     * @author ${author}
     * @date ${date}
     */
    @KoiLog(title = "${tableComment}删除", operType = OperType.DELETE)
    @SaCheckPermission("${moduleFolder}:${pageFolder}:delete")
    @PostMapping("/${pageFolder}/deleteById/{id}")
    public void delete(@PathVariable("id") Long id) {
        YUtil.isTrue(!${tableNameSmall}Service.removeById(id), "删除失败，请稍后重试");
    }

    /**
     * @description [批量删除]
     * @author ${author}
     * @date ${date}
     */
    @KoiLog(title = "${tableComment}批量删除", operType = OperType.DELETE)
    @SaCheckPermission("${moduleFolder}:${pageFolder}:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/${pageFolder}/batchDelete")
    public void batchDelete(@RequestBody List<Long> ids) {
        YUtil.isTrue(!${tableNameSmall}Service.removeBatchByIds(ids), "删除失败，请稍后重试");
    }

}
